package cn.easier.activity.console.controller;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 登录、首页Controller
 */
@Controller
@RequestMapping
public class IndexController {

    Logger logger = LoggerFactory.getLogger(IndexController.class);

    /**
     * 首页
     *
     * @return
     */
    @GetMapping("/home")
    public ModelAndView index(@AuthenticationPrincipal UserDetails userDetails) throws Exception {
        String user = "No Authentication";
        if (userDetails != null && (!StringUtils.isBlank(userDetails.getUsername()))) {
            user = userDetails.getUsername();
        }
        logger.info("用户请求首页：username =>{}", user);
        return new ModelAndView("/index");
    }

    /**
     * 登录页
     *
     * @return
     */
    @GetMapping("/sign-in")
    public ModelAndView signIn() {
        return new ModelAndView("/login");
    }
}
